package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.CustomerPageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.BusCustomer;
import com.southwind.mapper.BusCustomerMapper;
import com.southwind.service.BusCustomerService;
import com.southwind.vo.StatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-05-28
 */
@RestController
@RequestMapping("/customer")
public class BusCustomerController {

    @Autowired
    private BusCustomerMapper customerMapper;
    @Autowired
    private BusCustomerService customerService;

    @GetMapping("/areaStat")
    public List<StatVO> areaStat(){
        return this.customerMapper.areaStat();
    }

    @GetMapping("/list")
    public Map list(CustomerPageModel customerPageModel){
        QueryWrapper<BusCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(customerPageModel.getCustname()), "custname",customerPageModel.getCustname())
                .like(StringUtils.isNotBlank(customerPageModel.getIdentity()), "identity", customerPageModel.getIdentity());
        Page<BusCustomer> page = new Page<>(customerPageModel.getPage(), customerPageModel.getLimit());
        Page<BusCustomer> resultPage = this.customerService.page(page,queryWrapper);
        return ResultObj.resultData(resultPage.getRecords(),resultPage.getTotal());
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody BusCustomer customer){
        //身份证号唯一性校验
        QueryWrapper<BusCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity", customer.getIdentity());
        BusCustomer busCustomer = this.customerService.getOne(queryWrapper);
        if(busCustomer != null) return ResultObj.CUSTOMER_IDENTITY_ERROR;
        boolean save = this.customerService.save(customer);
        if(save) return ResultObj.CUSTOMER_ADD_SUCCESS;
        return ResultObj.CUSTOMER_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody BusCustomer customer){
        boolean updateById = this.customerService.updateById(customer);
        if(updateById) return ResultObj.CUSTOMER_UPDATE_SUCCESS;
        return ResultObj.CUSTOMER_UPDATE_ERROR;
    }

    @DeleteMapping("/delete")
    public ResultObj delete(String identity){
        boolean removeById = this.customerService.removeById(identity);
        if (removeById) return ResultObj.CUSTOMER_DELETE_SUCCESS;
        return ResultObj.CUSTOMER_DELETE_ERROR;
    }

    @GetMapping("/areaSexStat")
    public List<StatVO> areaSexStat(String area){
        return this.customerMapper.sexStat(area);
    }
}

